﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace Storage
{
   public class MySQLDriver
    {

       private string connectstr = "Database=storage;Data Source=localhost;User Id=root;Password=p715vuiw";
       MySqlConnection conn = null;

       public MySqlConnection Conn
       {
           get { return conn; }
           set { conn = value; }
       }
        MySqlDataReader reader = null;
        static MySQLDriver myInstance = null;
        MySQLDriver()
        {
            try
            {
            conn = new MySqlConnection(connectstr);
            conn.Open();
            }
            catch (MySqlException ex)
            {
                connectstr = "Data Source=localhost;User Id=root;Password=p715vuiw";
                conn = new MySqlConnection(connectstr);
                conn.Open();
                DBScript scr= new DBScript();
                string command = DBScript.str;
                MySqlCommand MyCommand = new MySqlCommand(command, conn);
                MyCommand.ExecuteNonQuery();
                conn.Close();
                connectstr = "Database=storage;Data Source=localhost;User Id=root;Password=p715vuiw";
                conn = new MySqlConnection(connectstr);
                conn.Open();
                conn.Close();
            }
        }
        public static MySQLDriver GetDriver
        {
            get
            {
                if (myInstance == null)
                {
                    myInstance = new MySQLDriver();
                }
                return myInstance;
            }
        }
        public void OpenConnection()
        {
            conn = new MySqlConnection(connectstr);
            conn.Open();
        }
        public void CloseConnection()
        {
            conn.Close();
        }
        public MySqlDataReader Read(string command)
        {
            GetDriver.OpenConnection();
            MySqlCommand MyCommand = new MySqlCommand(command, conn);
            reader = MyCommand.ExecuteReader();
            return reader;
        }
        public int ExecuteCommand(string command)
        {
            GetDriver.OpenConnection();
            MySqlCommand MyCommand = new MySqlCommand(command,conn);
            return MyCommand.ExecuteNonQuery();
        }
    }
}
